package com.duqc.tag.service.impl;

import cn.hutool.json.JSONUtil;
import com.duqc.tag.dao.TagMapper;
import com.duqc.tag.entity.Tag;
import com.duqc.tag.entity.TagRule;
import com.duqc.tag.service.TagRuleService;
import com.duqc.tag.service.TagService;
import com.duqc.util.SnowflakeIdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName TagServiceImpl
 * @Description TagServiceImpl
 * @Author duqc
 * @Date 2023/2/8
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class TagServiceImpl implements TagService {

    private final TagMapper tagMapper;

    private final TagRuleService tagRuleService;

    /**
     * 保存标签信息
     *
     * @param tag 标签信息
     * @return ResultMsg
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(Tag tag) {
        long nextId = SnowflakeIdUtil.nextId();
        tag.setTagId(nextId);
        //保存标签基本信息
        this.tagMapper.add(tag);
        //保存规则信息
        TagRule tagRule = new TagRule();
        tagRule.setTagId(nextId);
        tagRule.setRuleId(SnowflakeIdUtil.nextId());
        tagRule.setTagRule(JSONUtil.toJsonStr(tag.getTagRule()));
        this.tagRuleService.add(tagRule);
        return 1;
    }

}
